package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "分类管理接口")
@RestController
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增分类")
    public Result insertCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类，{}", categoryDTO);
        categoryService.insertCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 分类分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */

    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> queryPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分类分页查询，{}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.queryPage(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 修改分类状态
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改分类状态")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("修改分类状态,{},{}", status, id);
        categoryService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 按照类型查询分类
     *
     * @param type 分类类型
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("按照类型查询分类")
    public Result<List<Category>> queryByType(Integer type) {
        log.info("按照类型查询分类:{}", type);
        List<Category> list = categoryService.queryByType(type);
        return Result.success(list);
    }


    /**
     * 修改分类
     *
     * @param categoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类,{}", categoryDTO);
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 根据id删除
     * 这边要注意:一定要先判断这个套餐下面没有菜品了,才能删除这个套餐
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除")
    public Result deleteById(Long id){
        log.info("根据id删除,{}",id);
        categoryService.deleteById(id);
        return Result.success();

    }


}
